package java07;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class zy04 {
    public static void main(String[] args) {
        File src = new File("d:\\aaa\\src");
        File dest = new File("d:\\aaa\\dess");
        try {
            copyDirectory(src, dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void copyDirectory(File src, File dest) throws IOException {
        if (!dest.exists()) {
            dest.mkdir();
        }

        for (File file : src.listFiles()) {
            File destFile = new File(dest, file.getName());
            if (file.isDirectory()) {
                copyDirectory(file, destFile);
            } else {
                copyFile(file, destFile);
            }
        }
    }

    private static void copyFile(File src, File dest) throws IOException {
        try (FileInputStream fis = new FileInputStream(src);
             FileOutputStream fos = new FileOutputStream(dest);
             FileChannel srcChannel = fis.getChannel();
             FileChannel destChannel = fos.getChannel()) {

            destChannel.transferFrom(srcChannel, 0, srcChannel.size());
        }
    }
}
